package com.demo.controller;


import com.demo.dto.AnswerDTO;
import com.demo.dto.Result;
import com.demo.entity.Answer;
import com.demo.service.AnswerService;
import com.demo.vo.AnswerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zeng
 * @since 2022-03-23
 */
@Api(tags = "回答模块")
@RestController
@RequestMapping("/answer")
public class AnswerController {
    @Resource
    AnswerService answerService;

    @PostMapping("/")
    @ApiOperation("新增回答")
    Result<Answer> postAnswer(@RequestBody AnswerDTO answerDTO) {
        return answerService.postAnswer(answerDTO);
    }

    @DeleteMapping("/{answer_id}")
    @ApiOperation("删除回答")
    Result<Answer> deleteAnswer(@PathVariable("answer_id") Integer answerId) {
        return answerService.deleteAnswer(answerId);
    }

    @PutMapping("/{answer_id}")
    @ApiOperation("更新回答")
    Result<Answer> updateAnswer(@PathVariable("answer_id") Integer answerId, @RequestBody AnswerDTO answerDTO) {
        return answerService.updateAnswer(answerId, answerDTO);
    }

    @GetMapping("/common/queryAnswersByQuestionId")
    @ApiOperation("获取某问题下的回答列表（结果分页）")
    Result<List<AnswerVO>> queryAnswersByQuestionId (
            @RequestParam("question_id") String questionId,
            @RequestParam(value = "current", defaultValue = "1") Integer current
    ) {
        return answerService.queryAnswersByQuestionId(questionId, current);
    }

    @GetMapping("/common/{answer_id}")
    @ApiOperation("获取单个回答")
    Result<Answer> get(@PathVariable("answer_id") String answerId) {
        Answer answer = answerService.getById(answerId);
        if (answer == null) {
            return Result.fail("回答不存在");
        }
        return Result.ok(answer);
    }

    @GetMapping("/common/queryAnswersByUserId")
    @ApiOperation("根据用户id查询对应的回答列表")
    Result<List<AnswerVO>> queryAnswersByUserId (
            @RequestParam("question_id") Integer userId
    ) {
        return answerService.queryAnswersByUserId(userId);
    }
}

